package com.example.gzysmartcity.viewmodel;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.blankj.utilcode.util.ThreadUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.example.gzysmartcity.repository.model.Line;
import com.example.gzysmartcity.repository.model.Position;
import com.example.gzysmartcity.repository.model.PositionUesr;
import com.example.gzysmartcity.repository.remote.State;
import com.example.gzysmartcity.utils.NetworkUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class PositionViewModel extends ViewModel {
    MutableLiveData<State> positionLiveData = new MutableLiveData<>();
    List<Position.DataBean> positionList;
    List<Line.DataBean> lineList;
    List<PositionUesr> mPositionUesrList;

    public void getPositionLinesData() {
        //生产线
        ThreadUtils.executeByCached(new ThreadUtils.Task<Line>() {
            @Override
            public Line doInBackground() throws Throwable {
                return NetworkUtil.getInstance().get("Line/getAll?", null, Line.class, 0);
            }

            @Override
            public void onSuccess(Line result) {
                lineList = result.getData();
                getPositionsData();
            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onFail(Throwable t) {

            }
        });
    }

    public void getPositionsData() {
        ThreadUtils.executeByCached(new ThreadUtils.Task<Position>() {
            @Override
            public Position doInBackground() throws Throwable {
                return NetworkUtil.getInstance().get("Position/getAll?", null, Position.class, 0);
            }

            @Override
            public void onSuccess(Position result) {
                positionList = result.getData();
                getPositionUsersData();
            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onFail(Throwable t) {

            }
        });
    }

    public void getPositionUsersData() {
        ThreadUtils.executeByCached(new ThreadUtils.SimpleTask<Object>() {
            @Override
            public Object doInBackground() {
                mPositionUesrList = new ArrayList<>();
                for (Position.DataBean d : positionList) {
                    String lineName = lineList.stream().filter(a -> a.getId() == d.getLineId()).findFirst().get().getLineName();
                    mPositionUesrList.add(new PositionUesr(d.getPositionName(), lineName, d.getType(), d.getNum(), TimeUtils.getNowMills()));
                }
                return null;
            }

            @Override
            public void onSuccess(Object result) {
                positionLiveData.postValue(State.SUCCESS);
            }
        });
    }


    public List<PositionUesr> getPositionsSreach(String name) {
        if (name.equals(""))
            return mPositionUesrList;
        return mPositionUesrList.stream().filter(a -> a.getPositionName().contains(name)).collect(Collectors.toList());
    }

    public MutableLiveData<State> getPositionsLiveData() {
        return positionLiveData;
    }

    public List<PositionUesr> getPositionUesrList() {
        return mPositionUesrList;
    }

    public List<Position.DataBean> getPositionList() {
        return positionList;
    }

    public List<Line.DataBean> getLinesList() {
        return lineList;
    }
}
